﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using ComicIsland.App.Views.CacheManage;
using ComicIsland.Common;

/**
 * 创建时间：2015/1/12 14:35:39
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.App.CacheManageConverter
{
    public class ComicChapterTotalConverter : IValueConverter
    {
        /// <summary>
        /// 输出格式转换 
        /// <para>评论(10)</para>
        /// <para>人气：10</para>
        /// <para>评论：10</para>
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                string format = parameter.ToString();
                int total = 0;
                string ids = string.Empty;
                if (value != null)
                {
                    ids = value.ToString();
                }

                if (!string.IsNullOrWhiteSpace(ids))
                {
                    //var cpIds = new ParseComicBookPartIds();
                    //cpIds.ParseCBId(comicAndPartId);
                    //if (cpIds.BookId > 0 && cpIds.ComicId > 0)
                    //{
                    var cpIds = ids.Split('#');
                    if (cpIds != null && cpIds.Length == 2)
                    {
                        int comicId = 0, bookId = 0;
                        int.TryParse(cpIds[0], out comicId);
                        int.TryParse(cpIds[1], out bookId);
                        if (comicId > 0 && bookId > 0 )
                        {
                            var items = App.dbContextHelper.GetAllChaptersByComicId(comicId, bookId);
                            if (items != null && items.Count > 0)
                            {
                                total = items.Count;
                            }
                        }
                    }
                }

                return string.Format(format, total);
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
                return "";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
